import BaseController from '../../core/controllers/Base.controller';
import storageApi from '../../core/storage/storage';

@Inject('$log', '$rootScope', 'patrolService', 'ngDialogService')
class PatrolController extends BaseController {
  constructor() {
    super();

    this.paginationService = this.patrolService;
    this.$setQueryCallback((res) => {
      this.patrolListData = res.rows;
    });

    this.mapParams = {
      orgId: $rootScope.orgId || storageApi.get('orgId'),
      type: '',
    };
    this.selectedIndex = 0;
    this.$paginationQuery();
  }

  $query(param) {
    if (param && param.patrolTimeStart.$valid && param.patrolTimeEnd.$valid
      && param.patrolTimeStart.$viewValue > param.patrolTimeEnd.$viewValue) {
      this.ngDialogService.alertError('巡查时间开始时间应小于结束时间');
      return;
    }
    this.$paginationQuery();
  }

  $reset() {
    this.mapParams = {
      orgId: this.$rootScope.orgId || storageApi.get('orgId'),
      patrolUserName: '',
      patrolTimeStart: null,
      patrolTimeEnd: null,
      type: '',
    };
  }

  $delete(id) {
    this.ngDialogService.confirm('确定删除此数据吗？', id)
      .then((data) => {
        this.$log.debug(data);
        this.patrolService
          .delete([id])
          .then((res) => {
            this.$paginationQuery();
          });
      });
  }
}

export default PatrolController;
